package com.daiyang.lottery.dao.mapper;

import com.daiyang.lottery.dao.dataobject.Encrypt;
import com.daiyang.lottery.dao.dataobject.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {


    /**
     * 获取邮件绑定人数
     * @param email
     * @return
     */

    // 查询邮箱是被使用
    @Select("SELECT COUNT(*) FROM user WHERE email=#{email};")
    int CountByEmail(@Param("email") String email);

    /**
     * 获取手机号绑定人数
     * @param PhoneNumber
     * @return
     */
    // 查询手机号是被使用
    @Select("SELECT COUNT(*) FROM user WHERE phone_number=#{phoneNumber};")
    int CountByPhoneNumber(@Param("phoneNumber")Encrypt PhoneNumber);

    /**
     * 插入用户
     * @param userDO
     */
    @Insert("INSERT INTO user(user_name, email, phone_number, password, identity) "
    +" VALUES(#{userName}, #{email}, #{phoneNumber}, #{password}, #{identity})")
    @Options(useGeneratedKeys=true, keyProperty="id" ,keyColumn="id")
    void insert(UserDO userDO);


    @Select( "SELECT * FROM user WHERE phone_number=#{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);


    @Select("SELECT * FROM user WHERE email=#{email}")
    UserDO selectByEmail(@Param("email") String email);
    @Select("<script>"
            +" SELECT * FROM user"
            +" <if test= \"identity!=null\">"
            +" where identity = #{identity}"
            +" </if> "
            +" order by id desc "
            +" </script>")
    List<UserDO> selectUserList(@Param("identity") String identity);

    @Select("<script>"+
            " select id from user " +
            " where id in " +
            " <foreach  item='item'  collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            "</script>")

    List<Long> selectExistByIds(@Param("items") List<Long> Ids);

    @Select("<script>"+
            " select * from user " +
            " where id in " +
            " <foreach  item='item'  collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            "</script>")
    List<UserDO> bathcSelectByIds(@Param("items") List<Long> items);
}
